/**
 * 
 */
package com.p2p.base.interceptors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.cache.Cache;
import com.p2p.base.util.LoginUtil;

/**
 * spring mvc 登陆拦截器
 * 
 * @author leiruiqi
 *
 */

public class BaseLoginInterceptor extends HandlerInterceptorAdapter {


	protected String loginCookieName="yqh_ut";
	@Resource
	protected Cache loginCache;

	protected String loginBackUrl;

	protected String loginCenterUrl;

	protected final Logger logger = LoggerFactory
			.getLogger(BaseLoginInterceptor.class);

	

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		LoginUtil.setCurrentUser(null);
		// User项目也需要响应图片
		boolean b = handler instanceof HandlerMethod;
		if (!b) {
			return true;
		}
		initCurrentUser(request, response);
		
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		Class<?> type = handlerMethod.getBeanType();
		// Method method = handlerMethod.getMethod();
		// 判断是否需有loginNeedless标记
		LoginNeedless loginNeedless = type.getAnnotation(LoginNeedless.class);
		if (loginNeedless == null) {
			loginNeedless = type.getSuperclass().getAnnotation(
					LoginNeedless.class);
		}
		if (loginNeedless == null) {
			loginNeedless = handlerMethod
					.getMethodAnnotation(LoginNeedless.class);
		}

		if (loginNeedless != null) {
			if (loginNeedless.loginType() == LoginNeedless.LoginType.NEED_CURRENT_USER) {
				checkLogin(request, response);
			}
			return true;
		}
		if (!checkLogin(request, response)) {
			response.sendRedirect(loginCenterUrl + "?backUrl=" + loginBackUrl);
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

		LoginUtil.setCurrentUser(null);
		super.afterCompletion(request, response, handler, ex);
	}

	public boolean checkLogin(HttpServletRequest req, HttpServletResponse res) {
		return LoginUtil.getCurrentUser() != null;
	}

	public void initCurrentUser(HttpServletRequest req, HttpServletResponse res) {
		String key = LoginUtil.getCookieValue(req, res, loginCookieName);
		if (StringUtils.isNotBlank(key)) {
			Object user =  loginCache.getValue(key);
			if (user != null) {
				LoginUtil.setCurrentUser(user);
				loginCache.add(key, user, 10 * 60);
			}
		}
	}

	public String getLoginCenterUrl() {
		return loginCenterUrl;
	}

	public void setLoginCenterUrl(String loginCenterUrl) {
		this.loginCenterUrl = loginCenterUrl;
	}

	public String getLoginBackUrl() {
		return loginBackUrl;
	}

	public void setLoginBackUrl(String loginBackUrl) {
		this.loginBackUrl = loginBackUrl;
	}

	public String getLoginCookieName() {
		return loginCookieName;
	}

	public void setLoginCookieName(String loginCookieName) {
		this.loginCookieName = loginCookieName;
	}

	
	
}
